﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace Controllers
{
    public class WeeklyController : BaseController
    {
        //分页查询
        public ActionResult Find(int start, int limit, string idStr, DateTime? startDate, DateTime? endDate)
        {
            int empId;
            IList<int> deptIdList = getAuthedDeptIds("Week", idStr, out empId);
            EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;

            IQueryable<WeeklyDTO> list = from r in ctx.Weeklies join s in ctx.SignFlows on
                                        new {r.FormNo,IsCurrent=1} equals new {s.FormNo,s.IsCurrent}
                                        into t
                                         from x in t.DefaultIfEmpty()
                                         where r.StatusId != 0 || r.EmpId == ebi.Id
                                         orderby r.FormNo descending
                                         select new WeeklyDTO
                                         {
                                             FormNo = r.FormNo,
                                             EmpId = r.EmpId,
                                             EmpName = r.Employee.Name,
                                             CreatDate=r.CreatDate,
                                             BeginDate=r.BeginDate,
                                             EndDate=r.EndDate,
                                             Summary = r.Summary,
                                             TxtPlan = r.TxtPlan,
                                             Suggestion = r.Suggestion,
                                             Inspiration = r.Inspiration,
                                             Improve = r.Improve,
                                             StatusId = r.StatusId,
                                             CheckEmpId = x.CheckEmpId
                                         };
            if (empId != 0)
            {
                list = list.Where(r => r.EmpId == empId);
            }
            else
            {
                list = list.Where(r => (from r1 in ctx.EmpDepPositions where deptIdList.ToArray().Contains(r1.DeptId) select r1.EmpId).Contains(r.EmpId));
            }
            if(startDate!=null)
            {
                list = list.Where(r => r.BeginDate >= startDate);
            }
            if(endDate!=null)
            {
                list = list.Where(r => r.EndDate <= endDate);
            }
            CJson CJson = new CJson();
            string json = CJson.ToJsonString("totalCount:" + list.Count() + ",data", list.Skip(start).Take(limit).ToList());
            return Content(json);
        }

        public ActionResult Delete(string formNo)
        {
            var rdto = new ResultMessages();
            rdto.Message = "";
            try
            {
                //删除之前的签核数据
                var list = from r in ctx.SignFlows where r.FormNo == formNo select r;
                ctx.SignFlows.DeleteAllOnSubmit(list);
                var oriObj = ctx.Weeklies.Single(r => r.FormNo ==formNo);

                ctx.Weeklies.DeleteOnSubmit(oriObj);
                ctx.SubmitChanges();
                rdto.Message = "删除成功";
                rdto.Result = true;
            }
            catch (Exception ex)
            {
                rdto.Message = "删除失败";
                rdto.Result = false;
            }
            return Json(rdto);
        }
    }
}